package demo3

import (
	"bufio"
	"bytes"
	"io"
	"os"
)

func appendContent(filePath string, data []byte, pos int64) (int, error) {

	file, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE, 0x777)
	if err != nil {
		return -1, err
	}
	defer file.Close()

	ret, err := file.Seek(pos, io.SeekStart)
	if err != nil {
		return -1, err
	}
	buffer := bytes.NewBuffer(nil)
	writer := bufio.NewWriter(buffer)
	wl, err := io.Copy(writer, file)
	if err != nil {
		return -1, err
	}
	d := buffer.Bytes()[:wl]
	ret, err = file.Seek(ret, io.SeekStart)
	data = append(data, d...)
	return file.Write(data)
}
